private void button1_Click(object sender, EventArgs e)
{
SRE.SetInputToDefaultAudioDevice(); // <======= 默认的语音输入设备,你可以设定为去识别一个WAV文件。
GrammarBuilder GB = new GrammarBuilder();
//GB.Append("选择");
GB.Append(new Choices(new string[] { "换色","红色", "绿色", "蓝色", "紫色", "黑色",
"洗澡","睡觉","你好","你来了","再见","晚安","音乐","播放","停止","关闭","换图","美女","下一张","图片","二宝","狗蛋"}));
Grammar G = new Grammar(GB);
G.SpeechRecognized = new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized);
SRE.LoadGrammar(G); //SRE.RecognizeAsync(RecognizeMode.Multiple); //<======= 异步调用识别引擎,允许多次识别(否则程序只响应你的一句话)
SRE.RecognizeAsync();
}
// Handle the AudioStateChanged event.
private void AudioStateChangedHandler(object sender, AudioStateChangedEventArgs e)
{
//addLogT( string.Format("当前状态 ({0}): {1}", DateTime.Now.ToString("mm:ss.f"), e.AudioState));
} // Handle the RecognizeCompleted event.
private void RecognizeCompletedHandler(object sender, RecognizeCompletedEventArgs e)
{
//addLogT(string.Format("识别完成 ({0}):",DateTime.Now.ToString("mm:ss.f"))); string resultText;
if (e.Result != null) { resultText = e.Result.Text; }
else { resultText = "<null>"; } //Console.WriteLine(" BabbleTimeout: {0}; InitialSilenceTimeout: {1}; Result text: {2}",
// e.BabbleTimeout, e.InitialSilenceTimeout, resultText);
if (e.Error != null)
{
Console.WriteLine(" Exception message: ", e.Error.Message);
} // Start the next asynchronous recognition operation.
((SpeechRecognitionEngine)sender).RecognizeAsync(RecognizeMode.Single);
}
评论